#include "TitleScene.h"
#include <DxLib.h>
#include"SceneManager.h"
#include"OpeningScene.h"
#include "../Graphics/BaseDrawer.h"
#include "../Graphics/FragmentDrawer.h"
#include"../3D/Model3D.h"
#include "../Sound/BGMManager.h"
#include "../Sound/SoundEffect.h"

#include"../application.h"

TitleScene::TitleScene(void){
	_updater = &TitleScene::UpdatePre;
	_fragmentDrawer.Reset(new FragmentDrawer());
}


TitleScene::~TitleScene(void){

}

void
TitleScene::UpdatePre(SceneManager& caller){
	const Size& size = Application::Instance().WindowSize();
	_capHandle=DxLib::MakeGraph(size.width,size.height);
	_baseTitle.Reset(new BaseDrawer("Resource/img/title/title.png"));
	_baseTitle->SetLocation(loc_center);
	_baseTitle->MovePosition(0,-100);

	_bodyeater.Reset(new Model3D("model/bodyeater/bodyeater.mv1"));
	_bodyeater->ChangeAnimation(1,true);
	_bodyeater->Move(Vector3D(900,100,50));
	_bodyeater->SetVelocity(Vector3D(-5,0,0));
	_bodyeater->SetScale(30.f);
	_bodyeater->RotateY(3.14f/2.f);
	SetGlobalAmbientLight( GetColorF( 1.0f, 0.5f, 0.5f, 1.0f ) ) ;

	_glassBreakSE.Reset(new SoundEffect("snd/osb/glass_break.wav"));

	_updater=&TitleScene::UpdateMain;
}

void
TitleScene::UpdateMain(SceneManager& caller){
	_baseTitle->Draw();
	if(_bodyeater->Position().x<-1000){
		_bodyeater->Move(Vector3D(2000,0,0));
	}
	_bodyeater->Update();
	DrawFormatString(0,0,0xFFFFFFFF,"%f",_bodyeater->Position().x);
	if(CheckNextRequest_Debug()){	
		_timer=45;
		float xv=(400-_bodyeater->Position().x)/_timer;

		_bodyeater->SetVelocity(Vector3D(xv,28,-10));
		_bodyeater->SetAccelaration(Vector3D(0,-0.96f,0));
		Vector3D& v= _bodyeater->Velocity();
		_bodyeater->RotateX(atan2(v.y,v.z));
		
		_updater=&TitleScene::UpdateJumping;
		BGMManager::Instance().FadeOut();
	}
}

void
TitleScene::UpdateJumping(SceneManager& caller){
	_baseTitle->Draw();
	Vector3D& v= _bodyeater->Velocity();
	_bodyeater->RotateX(atan2(v.y,-v.z));
	_bodyeater->Update();
	if(--_timer==0){
		_timer=60;
		const Size& size=Application::Instance().WindowSize();
		int result = DxLib::GetDrawScreenGraph(0,0,size.width,size.height,_capHandle);
		DxLib::SetDrawBlendMode(DX_BLENDMODE_INVDESTCOLOR,255);
		_updater=&TitleScene::UpdateChanging;
	}
}

void
TitleScene::UpdateChanging(SceneManager& caller){
	DrawGraph(0,0,_capHandle,false);
	const Size& size=Application::Instance().WindowSize();
	DrawBox(0,0,size.width,size.height,GetColor(255,255,255),true);
	if(--_timer==0){
		SetDrawBlendMode(DX_BLENDMODE_NOBLEND,255);
		_timer=120;
		_fragmentDrawer->Capture();
		_fragmentDrawer->Break();
		_updater=&TitleScene::UpdateBreaking;
		_glassBreakSE->Play();
	}
}

void
TitleScene::UpdateBreaking(SceneManager& caller){
	_fragmentDrawer->Draw();
	if(--_timer==0){
		caller.ChangeScene(new OpeningScene());
	}
}

void
TitleScene::Update(SceneManager& caller){
	(this->*_updater)(caller);
}
